﻿from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from pathlib import Path
from fastapi.middleware.cors import CORSMiddleware
# 从公共配置模块导入配置
from config import BASE_URL, UPLOAD_DIR, add_memory_route

# 确保目录存在
Path(UPLOAD_DIR).mkdir(parents=True, exist_ok=True)

# 创建应用
app = FastAPI(title="随手工具箱")
# 配置CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 允许所有源，生产环境建议指定具体域名
    allow_credentials=True,
    allow_methods=["*"],  # 允许所有HTTP方法
    allow_headers=["*"],  # 允许所有头信息
)

# 静态文件配置
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="static")

# 导入路由模块
import server
import paddleocrPdfDeal

# 注册路由
app.include_router(server.router)
app.include_router(paddleocrPdfDeal.router_paddle_ocr)

# 注册内存监控路由
add_memory_route(app)

# 添加应用健康检查端点
@app.get("/health")
def health_check():
    """应用健康检查端点"""
    from config import get_memory_info
    mem_info = get_memory_info()
    status = "healthy" if mem_info.get("status") != "critical" else "warning"
    return {
        "status": status,
        "memory": {
            "available_mb": mem_info.get("available_mb", 0),
            "total_mb": mem_info.get("total_mb", 0),
            "available_percent": mem_info.get("available_percent", 0)
        },
        "uptime": "运行正常"
    }

if __name__ == "__main__":
    import uvicorn
    uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True)
